﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Globalization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class ConditionTypeConverter
        Inherits TypeConverter
        ' Methods
        Public Sub New()
            Dim fullName As String = ConditionTypeConverter.CodeConditionType.FullName
            Dim customAttributes As Object() = ConditionTypeConverter.CodeConditionType.GetCustomAttributes(GetType(DisplayNameAttribute), False)
            If (((Not customAttributes Is Nothing) AndAlso (customAttributes.Length > 0)) AndAlso TypeOf customAttributes(0) Is DisplayNameAttribute) Then
                fullName = DirectCast(customAttributes(0), DisplayNameAttribute).DisplayName
            End If
            Me.conditionDecls.Add(fullName, ConditionTypeConverter.CodeConditionType)
            fullName = ConditionTypeConverter.RuleDefinitionsType.FullName
            customAttributes = ConditionTypeConverter.RuleDefinitionsType.GetCustomAttributes(GetType(DisplayNameAttribute), False)
            If (((Not customAttributes Is Nothing) AndAlso (customAttributes.Length > 0)) AndAlso TypeOf customAttributes(0) Is DisplayNameAttribute) Then
                fullName = DirectCast(customAttributes(0), DisplayNameAttribute).DisplayName
            End If
            Me.conditionDecls.Add(fullName, ConditionTypeConverter.RuleDefinitionsType)
        End Sub

        Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            Return ((sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
        End Function

        Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
            Return ((destinationType Is GetType(String)) OrElse MyBase.CanConvertTo(context, destinationType))
        End Function

        Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
            If Not TypeOf value Is String Then
                Return MyBase.ConvertFrom(context, culture, value)
            End If
            If ((CStr(value).Length <> 0) AndAlso Not (CStr(value) = SR.GetString("NullConditionExpression"))) Then
                Return Activator.CreateInstance([TryCast](Me.conditionDecls.Item(value),Type))
            End If
            Return Nothing
        End Function

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            If (value Is Nothing) Then
                Return SR.GetString("NullConditionExpression")
            End If
            Dim key As Object = Nothing
            If ((destinationType Is GetType(String)) AndAlso TypeOf value Is ActivityCondition) Then
                Dim entry As DictionaryEntry
                For Each entry In Me.conditionDecls
                    If (value.GetType Is entry.Value) Then
                        key = entry.Key
                        Exit For
                    End If
                Next
            End If
            If (key Is Nothing) Then
                key = MyBase.ConvertTo(context, culture, value, destinationType)
            End If
            Return key
        End Function

        Public Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
            Dim descriptors As New PropertyDescriptorCollection(New PropertyDescriptor(0  - 1) {})
            Dim converter As TypeConverter = TypeDescriptor.GetConverter(value.GetType)
            If (((Not converter Is Nothing) AndAlso (Not converter.GetType Is MyBase.GetType)) AndAlso converter.GetPropertiesSupported) Then
                Return converter.GetProperties(context, value, attributes)
            End If
            Dim component As IComponent = PropertyDescriptorUtils.GetComponent(context)
            If (Not component Is Nothing) Then
                descriptors = PropertyDescriptorFilter.FilterProperties(component.Site, value, TypeDescriptor.GetProperties(value, New Attribute() { BrowsableAttribute.Yes }))
            End If
            Return descriptors
        End Function

        Public Overrides Function GetPropertiesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

        Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
            Dim list As New ArrayList
            list.Add(Nothing)
            Dim obj2 As Object
            For Each obj2 In Me.conditionDecls.Keys
                Dim type As Type = [TryCast](Me.conditionDecls.Item(obj2),Type)
                list.Add(Activator.CreateInstance(type))
            Next
            Return New StandardValuesCollection(DirectCast(list.ToArray(GetType(ActivityCondition)), ActivityCondition()))
        End Function

        Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

        Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function


        ' Fields
        Friend Shared ReadOnly CodeConditionType As Type = Type.GetType("System.Workflow.Activities.CodeCondition, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
        Private conditionDecls As Hashtable = New Hashtable
        Friend Shared DeclarativeConditionDynamicProp As DependencyProperty = DirectCast(ConditionTypeConverter.RuleConditionReferenceType.GetField("RuleDefinitionsProperty").GetValue(Nothing), DependencyProperty)
        Friend Shared ReadOnly RuleConditionReferenceType As Type = Type.GetType("System.Workflow.Activities.Rules.RuleDefinitions, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
        Friend Shared ReadOnly RuleDefinitionsType As Type = Type.GetType("System.Workflow.Activities.Rules.RuleConditionReference, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
    End Class
End Namespace

